package com.example.demo.service.impl;

import com.example.demo.mapper.RoomReserveMapper;
import com.example.demo.pojo.RoomReserve;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * (RoomReserve)表服务实现类
 *
 * @author makejava
 * @since 2025-01-19 21:37:57
 */
@Service("roomReserveService")
public class RoomReserveServiceImpl {

    private static final Logger logger = LoggerFactory.getLogger(RoomReserveServiceImpl.class);

    @Resource
    private RoomReserveMapper roomReserveMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param reservationId 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer reservationId) {
        Map<String, Object> map = new HashMap<>();
        try {
            RoomReserve roomReserve = this.roomReserveMapper.selectById(reservationId);
            map.put("code", 200);
            map.put("data", roomReserve);
            map.put("msg", "查询单条记录成功");
        } catch (Exception e) {
            logger.error("查询单条记录失败，ID: {}", reservationId, e);
            map.put("code", 500);
            map.put("msg", "查询单条记录失败");
        }
        return map;
    }

    /**
     * 一次性全部查询
     *
     * @return 查询结果
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<RoomReserve> roomReserves = this.roomReserveMapper.selectAll();
            map.put("code", 200);
            map.put("msg", "全部查询成功");
            map.put("data", roomReserves);
        } catch (Exception e) {
            logger.error("全部查询失败", e);
            map.put("code", 500);
            map.put("msg", "全部查询失败");
        }
        return map;
    }

    /**
     * 分页查询
     *
     * @param roomReserve 筛选条件
     * @param page        要查询第几页
     * @param limit       每页显示多少条
     * @return 查询结果
     */
    public Map<String, Object> selectByLimit(RoomReserve roomReserve, int page, int limit) {
        Map<String, Object> map = new HashMap<>();
        try {
            // page 页码   (page-1)*limit=起始下标index
            int startIndex = (page - 1) * limit;   // 计算出起始下标
            List<RoomReserve> list = this.roomReserveMapper.selectByLimit(roomReserve, startIndex, limit);

            // 获取满足条件的总条数
            long count = this.roomReserveMapper.selectCount(roomReserve);
            // 计算总页数
            long maxPage = (count - 1) / limit + 1;

            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("count", count);
            map.put("maxPage", maxPage);
            map.put("nowPage", page);
            map.put("data", list);
        } catch (Exception e) {
            logger.error("分页查询失败，页码: {}, 每页条数: {}", page, limit, e);
            map.put("code", 500);
            map.put("msg", "分页查询失败");
        }
        return map;
    }

    /**
     * 新增数据
     *
     * @param roomReserve 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(RoomReserve roomReserve) {
        Map<String, Object> map = new HashMap<>();
        try {
            this.roomReserveMapper.insert(roomReserve);
            map.put("code", 200);
            map.put("data", roomReserve);
            map.put("msg", "新增成功");
        } catch (Exception e) {
            logger.error("新增数据失败", e);
            map.put("code", 500);
            map.put("msg", "新增数据失败");
        }
        return map;
    }

    /**
     * 修改数据
     *
     * @param roomReserve 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateById(RoomReserve roomReserve) {
        Map<String, Object> map = new HashMap<>();
        try {
            this.roomReserveMapper.updateById(roomReserve);
            map.put("code", 200);
            map.put("data", roomReserve);
            map.put("msg", "更新成功");
        } catch (Exception e) {
            logger.error("更新数据失败", e);
            map.put("code", 500);
            map.put("msg", "更新数据失败");
        }
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param reservationId 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteById(Integer reservationId) {
        Map<String, Object> map = new HashMap<>();
        try {
            int result = this.roomReserveMapper.deleteById(reservationId);
            map.put("code", 200);
            map.put("id", reservationId);
            map.put("msg", result > 0 ? "删除成功" : "删除失败");
        } catch (Exception e) {
            logger.error("删除数据失败，ID: {}", reservationId, e);
            map.put("code", 500);
            map.put("msg", "删除数据失败");
        }
        return map;
    }

    /**
     * 将记录添加到已入住房间订单
     *
     * @param roomReserve 实体
     * @return 添加结果
     */
    public Map<String, Object> addToCheckinList(RoomReserve roomReserve) {
        Map<String, Object> map = new HashMap<>();
        try {
            // 这里需要实现将记录添加到已入住房间订单的逻辑
            // 例如：调用相应的Mapper方法
            // int rows = roomReserveMapper.addToCheckinList(roomReserve);
            // if (rows > 0) {
            //     map.put("code", 200);
            //     map.put("msg", "已成功添加到已入住房间订单");
            // } else {
            //     map.put("code", 500);
            //     map.put("msg", "添加到已入住房间订单失败");
            // }
            // 暂时假设添加成功
            map.put("code", 200);
            map.put("msg", "已成功添加到已入住房间订单");
        } catch (Exception e) {
            logger.error("添加到已入住房间订单失败", e);
            map.put("code", 500);
            map.put("msg", "添加到已入住房间订单失败");
        }
        return map;
    }

    /**
     * 办理退房操作
     *
     * @param roomReserve 退房信息
     * @return 操作结果
     */
    public Map<String, Object> checkOut(RoomReserve roomReserve) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 退房逻辑
            result.put("code", 200);
            result.put("msg", "退房成功");
        } catch (Exception e) {
            logger.error("退房操作失败", e);
            result.put("code", 500);
            result.put("msg", "退房操作失败");
        }
        return result;
    }
}